#include "./include/regionSearchDlg.h"
#include "include/define.h"

#include <QtGui>
#include <QDebug>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>


RegionSearchDlg::RegionSearchDlg(QWidget *parent)
    : Search(parent)
{
    setFixedSize(800, 480);
    setWindowTitle(QString::fromUtf8("区域查询"));
    //setWindowFlags(Qt::FramelessWindowHint);

    myTimerId = 0;
    isEmpty = 0; //查询结果是否为空
    pageSeq = 1; //页序号
    numOfTotalItem = 0;

    CreateWidget();
    CreateLayout();

    SearchItems();
}

void RegionSearchDlg::CreateWidget()
{
    area = new QComboBox;
    ridgepole = new QComboBox;
    layer = new QComboBox;
    room = new QComboBox;
    devName = new QComboBox;

    areaLabel = new QLabel(QString::fromUtf8("区"), this);
    ridgepoleLabel = new QLabel(QString::fromUtf8("栋"), this);
    layerLabel = new QLabel(QString::fromUtf8("层"), this);
    roomLabel = new QLabel(QString::fromUtf8("房"), this);
    devNameLabel = new QLabel(QString::fromUtf8("设备"), this);
    tmpLabel = new QLabel(QString::fromUtf8("未找到结果"), this);

    pageUp = new QPushButton(QString::fromUtf8("上一页"), this);
    pageDown = new QPushButton(QString::fromUtf8("下一页"), this);

    quit = new QPushButton(QString::fromUtf8("退出"), this);

    groupBox = new QGroupBox(this);
    groupBox->setVisible(false);

    QFont tmpFont = tmpLabel->font();
    tmpFont.setPointSize(100);
    tmpLabel->setFont(tmpFont);
    tmpLabel->setAlignment(Qt::AlignCenter);

    area->addItem(QString::fromUtf8("A"));
    area->addItem(QString::fromUtf8("B"));
    ridgepole->addItem(" ");
    ridgepole->addItem(QString::fromUtf8("01"));
    ridgepole->addItem(QString::fromUtf8("02"));
    layer->addItem(" ");
    layer->addItem(QString::fromUtf8("01"));
    layer->addItem(QString::fromUtf8("02"));
    layer->addItem(QString::fromUtf8("03"));
    room->addItem(" ");
    room->addItem(QString::fromUtf8("101"));
    room->addItem(QString::fromUtf8("102"));
    room->addItem(QString::fromUtf8("201"));
    room->addItem(QString::fromUtf8("202"));
    room->addItem(QString::fromUtf8("301"));
    room->addItem(QString::fromUtf8("302"));
    devName->addItem(" ");
    devName->addItem(QString::fromUtf8("消防水泵阀"));
    devName->addItem(QString::fromUtf8("排烟阀"));

    connect(area, SIGNAL(currentIndexChanged(int)), this, SLOT(SearchItems()));
    connect(ridgepole, SIGNAL(currentIndexChanged(int)), this, SLOT(SearchItems()));
    connect(layer, SIGNAL(currentIndexChanged(int)), this, SLOT(SearchItems()));
    connect(room, SIGNAL(currentIndexChanged(int)), this, SLOT(SearchItems()));
    connect(devName, SIGNAL(currentIndexChanged(int)), this, SLOT(SearchItems()));
    connect(pageUp, SIGNAL(clicked()), this, SLOT(PageSeqDes()));
    connect(pageDown, SIGNAL(clicked()), this, SLOT(PageSeqInc()));
    connect(quit, SIGNAL(clicked()), this, SLOT(close()));
}

void RegionSearchDlg::CreateLayout()
{
    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(area);
    topLayout->addWidget(areaLabel);
    topLayout->addWidget(ridgepole);
    topLayout->addWidget(ridgepoleLabel);
    topLayout->addWidget(layer);
    topLayout->addWidget(layerLabel);
    topLayout->addWidget(room);
    topLayout->addWidget(roomLabel);
    topLayout->addWidget(devName);
    topLayout->addWidget(devNameLabel);
    topLayout->addStretch();
    topLayout->addWidget(pageUp);
    topLayout->addWidget(pageDown);
    topLayout->addWidget(quit);

    mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(tmpLabel);

    setLayout(mainLayout);
}

void RegionSearchDlg::SearchItems()
{
    QString query = BuildQuery();
    qDebug() << "查询语句：" << query;
    numOfTotalItem = GetNumOfTotalItem(query);
    qDebug() << "总的个数：" << numOfTotalItem;
    GetMaxPages();
    qDebug() << "最大页数：" << maxPageSeq;
    if(numOfTotalItem != 0)
    {
        UpdateSearchResult(query);
    }
}

QString RegionSearchDlg::BuildQuery()
{
    QString selectQuery = "";
    QString areaTemp = area->currentText() + QString::fromUtf8("区");
    QString ridgepoleTemp = ridgepole->currentText() +QString::fromUtf8("栋");
    QString layerTemp = layer->currentText() +QString::fromUtf8("层");
    QString roomTemp = room->currentText() +QString::fromUtf8("房");
    QString devNameTemp = devName->currentText();

    selectQuery += QString("area = '%1' ").arg(areaTemp);   //地区选项没有空

    if(ridgepole->currentText() != " ")
    {
        selectQuery += QString("and ridgepole = '%1' ").arg(ridgepoleTemp);
    }
    if(layer->currentText() != " ")
    {
        selectQuery += QString("and layer = '%1' ").arg(layerTemp);
    }
    if(room->currentText() != " ")
    {
        selectQuery += QString("and room = '%1' ").arg(roomTemp);
    }
    if(devName->currentText() != " ")
    {
        selectQuery += QString("and devName = '%1' ").arg(devNameTemp);
    }

    return selectQuery;
}

void RegionSearchDlg::mousePressEvent(QMouseEvent *)
{
    qDebug() << "主界面";
}

void RegionSearchDlg::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == myTimerId)
    {
        for(int i=0; i<20; i++)
        {
            if(item[i]->isClicked)
            {
                emit sendItemId(item[i]->id->text());
                killTimer(myTimerId);
                this->close();
            }
        }

    }
}
void RegionSearchDlg::showEvent(QShowEvent *)
{
    myTimerId = startTimer(100);

}

void RegionSearchDlg::UpdateSearchResult(const QString &sqlQuery)
{
    int i = 0;  //循环变量
    QString areaTemp = "";
    QString ridgepoleTemp = "";
    QString layerTemp = "";
    QString roomTemp = "";
    QString devNameTemp = "";

    QSqlDatabase::database().transaction();
    QSqlQuery query;

    query.exec(QString("select id, area, ridgepole, layer, room, devName from"
                       "(select * from itemInfo where %1) limit %2, %3"
                       ).arg(sqlQuery).arg((pageSeq-1)*20).arg(ITEM_NUM_OF_PERPAGE));
    while(query.next())
    {
        item[i] = new SearchItem(this);
        item[i]->SetId(query.value(0).toString());

        areaTemp = query.value(1).toString();
        ridgepoleTemp = query.value(2).toString();
        layerTemp = query.value(3).toString();
        roomTemp = query.value(4).toString();
        devNameTemp = query.value(5).toString();

        QString pos = areaTemp + ridgepoleTemp + layerTemp + roomTemp;
        item[i]->SetPos(pos);
        item[i]->SetIcon(devNameTemp);

        i++;
    }
    qDebug() << "i: " << i;

    count = i;
    bottomLayout = new QGridLayout;
    for(i=0; i<count; i++)
    {
        bottomLayout->addWidget(item[i], i/5, i%5);
    }

    if(count < ITEM_NUM_OF_PERPAGE)
    {
        for(; i<ITEM_NUM_OF_PERPAGE; i++)
        {
            item[i] = new SearchItem;
            bottomLayout->addWidget(item[i], i/5, i%5);
        }
    }

    if(i != 0)
    {
        if(isEmpty == 0)
        {
            qDebug() << "到了";
            tmpLabel->hide();
            groupBox->setVisible(true);
            groupBox->setLayout(bottomLayout);
            mainLayout->addWidget(groupBox);
        }
        else
        {
            tmpLabel->hide();
            mainLayout->removeWidget(groupBox);
            groupBox->close();
            groupBox = new QGroupBox(this);
            groupBox->setLayout(bottomLayout);
            mainLayout->addWidget(groupBox);
        }
        isEmpty = 1;
    }
    else
    {
        groupBox->close();
        tmpLabel->show();

    }
    QSqlDatabase::database().commit();
}
